﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ERP.Classes;

namespace ERP.Classes
{
    class csDuonglichQuaAmlich
    {
        int year;
        int month;
        int dayOfMonth;
        int dayOfWeek;
        public int chYear;
        public int chMonth;
        public Boolean chLeapMonth;
        public int chDay;
        public int[] chYearData = {0, 384, 739, 1093, 1477, 1831, 2185, 2569, 2923, 3278,3662, 4017, 4401, 4755, 5109, 5492, 5847, 6201, 6585, 6940,7324, 7678, 8033, 8416, 8770, 9125, 9509, 9863, 10218, 10602,10956, 11340, 11694, 12048, 12432, 12786, 13141, 13525, 13880, 14264,14618, 14972, 15356, 15710, 16064, 16449, 16803, 17158, 17542, 17896,18279, 18633, 18988, 19372, 19727, 20081, 20465, 20819, 21203, 21557,21911, 22295, 22650, 23004, 23389, 23743, 24097, 24481, 24835, 25219,25573, 25928, 26312, 26666, 27021, 27405, 27759, 28142, 28497, 28851,29235, 29590, 29944, 30328, 30683, 31036, 31420, 31775, 32159, 32513,32868, 33252, 33606, 33960, 34344, 34698, 35082, 35436, 35791, 36175,36530, 36884, 37268, 37622, 38006, 38360, 38714, 39098, 39453, 39808,40192, 40546, 40900, 41283, 41638, 42022, 42376, 42731, 43115, 43469,43823, 44207, 44561, 44916, 45300, 45654, 46039, 46393, 46747, 47131,47485, 47839, 48223, 48578, 48962, 49316, 49671, 50055, 50409, 50793,51147, 51502, 51886, 52240, 52594, 52978, 53332, 53686, 54070, 54425,54779, 55163, 55518, 55902, 56256, 56610, 56994, 57348, 57702, 58086,58441, 58796, 59180, 59534, 59918, 60272, 60626, 61010, 61364, 61719,62103, 62458, 62842, 63196, 63550, 63933, 64288, 64642, 65026
        };
        int[] chMonthData = {0x459444, 0x514444, 0x454410, 0x1456041, 0x511041, 0x444111, 0x1110645, 0x110451, 0x444451, 0x1114464,0x514444, 0x1516410, 0x544410, 0x511044, 0x444911, 0x444115, 0x111114, 0x444591, 0x445111, 0x1165104,0x151104, 0x544411, 0x1141844, 0x141045, 0x444145, 0x1111314, 0x111444, 0x451141, 0x1151124, 0x151104,0x1452411, 0x450411, 0x441045, 0x1104c45, 0x44451, 0x111445, 0x451461, 0x514441, 0x151c104, 0x544104,0x510411, 0x1441911, 0x441114, 0x44514, 0x1114711, 0x145110, 0x544441, 0x1544124, 0x544104, 0x512414,0x110445, 0x441115, 0x1045914, 0x451144, 0x145110, 0x5450c1, 0x151041, 0x114c105, 0x444105, 0x110445,0x443145, 0x444451, 0x111444, 0x1451311, 0x454410, 0x451041, 0x1444125, 0x444111, 0x1118451, 0x110514,0x444451, 0x1114c44, 0x115104, 0x514411, 0x15110c4, 0x511044, 0x464111, 0x444115, 0x111114, 0x446511,0x445111, 0x145104, 0x544611, 0x544411, 0x141044, 0x504195, 0x444145, 0x1119144, 0x111444, 0x451441,0x1151904, 0x451104, 0x450411, 0x1141245, 0x441045, 0x1124445, 0x44451, 0x114451, 0x453441, 0x514441,0x514104, 0x1510611, 0x510411, 0x1641051, 0x441114, 0x44514, 0x116511, 0x145111, 0x545041, 0x1544904,0x544104, 0x510414, 0x441194, 0x441115, 0x1125114, 0x51144, 0x145111, 0x1145841, 0x151041, 0x144105,0x1110645, 0x110445, 0x441145, 0x1111161, 0x111444, 0x1459111, 0x454410, 0x451041, 0x1444905, 0x444111,0x110451, 0x444651, 0x444511, 0x1194444, 0x115104, 0x514411, 0x1513044, 0x511044, 0x1110654, 0x111114,0x444511, 0x1114524, 0x145104, 0x145104, 0x546411, 0x150411, 0x141045, 0x504915, 0x504445, 0x111144,0x445194, 0x451441, 0x1159104, 0x451104, 0x450411, 0x1443045, 0x441045, 0x104445, 0x111945, 0x114451,0x451444, 0x1451124, 0x514104, 0x1518411, 0x510411, 0x441111, 0x1101c51, 0x104514, 0x114511, 0x5144c4,0x545041, 0x1564104, 0x544104, 0x510414, 0x443114, 0x441115, 0x105114, 0x145311, 0x51111 };

        private Boolean isLeapyear(int i) {
        return i % 4 == 0 && (i % 100 != 0 || i % 400 == 0);
        }

        private int numDayAfterOrigin(int i, int j, int k) {
            int[] dayElapsed = {0, 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365};
            int l = i--;
            return ((((j <= 2 || !isLeapyear(l) ? 0 : 1) + 365 * i + i / 4) - i / 100) + i / 400 + dayElapsed[j] + k) - 1;
        }

        private int cal_DayDifference(int i, int j, int k, int N, int T, int nam) {
        return numDayAfterOrigin(i, j, k) - numDayAfterOrigin(nam, T, N); }

        public void DuonglichQuaAmlich(int N,int T,int nam)
        {
            int i = 0;
            int j = chYearData.Length - 1;
            int l = 0;
            int j1 = -cal_DayDifference(1881, 1, 30, N, T, nam);
            do
            {
            int k = (i + j) / 2;
            if(j1 < chYearData[k])
            j = k - 1;
            else
            i = k + 1;
            }

            while(j1 != chYearData[j] && i <= j);
            chYear = j;
            chMonth = 1;
            chLeapMonth = false;
            j1 -= chYearData[chYear];
            l = chMonthData[chYear];
            int i1;
            while(j1 >= (i1 = 29 + l % 4 % 2))
            {
            j1 -= i1;
            l /= 4;
            if(l % 4 < 2)
            {
            chLeapMonth = false;
            chMonth++;
            }

            else
            {
            chLeapMonth = true;
            }

            }
            chDay = j1 + 1;
        }

        int getngay()
        {
        return chDay; //ngay;
        }

        int getthang()
        {
        return chMonth; //thang;
        }

        int getcan()
        {
        return (chYear +7) % 10; // can;
        }

        int getchi()
        {
        return ((chYear + 5) % 12); // chi;
        }
    }
}
